package com.scqkxx.gamebox.activity.myself.loginutils;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.scqkzqtz.base.library.utils.DrawableUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 输入验证
 * Created by zsx on 2018/8/14.
 */

public class InputVerificationUtils {
    private static final String regexPhoneNumber = "^(1[3-9])\\d{9}$";

    public static final String TAG_PHONENUMBER = "Tag_PhoneNumber";
    public static final String TAG_PASSWORD = "TAG_PassWord";
    public static final String TAG_SMSCODE = "Tag_smsCode";

    /**
     * 输入信息是否满足要求
     */
    public static void onPutListener(@NonNull TextView submit, EditText... editTexts) {
        for (EditText editText : editTexts) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (isPutMeet(editTexts))
                        submit.setBackground(DrawableUtil.getSolidRectDrawable(4, Color.parseColor("#E7BD5D")));
                    else
                        submit.setBackground(DrawableUtil.getSolidRectDrawable(4, Color.parseColor("#DBDBDB")));
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    }

    public static Boolean isPutMeet(EditText... editTexts) {
        int meetNumber = 0;
        for (EditText editText : editTexts) {
            if (editText.getTag() != null)
                switch (editText.getTag().toString()) {
                    case TAG_PHONENUMBER:
                        return isEmail(editText.getText().toString().trim());
//                        if (editText.getText().toString().trim().length() == 11) {
//                            meetNumber++;
//                        }
//                        break;
                    case TAG_SMSCODE:
                        if (editText.getText().toString().trim().length() == 6) {
                            meetNumber++;
                        }
                        break;

                    case TAG_PASSWORD:
                        if (editText.getText().toString().trim().length() >= 8 && editText.getText().toString().trim().length() <= 18) {
                            meetNumber++;
                        }
                        break;
                }
        }
        return meetNumber == editTexts.length;
    }


    public static boolean isEmail(String email){
        if (null==email || "".equals(email)) return false;
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static void psdShowHide(CheckBox checkBox, EditText editText) {
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                editText.setSelection(editText.getText().toString().length());
                if (isChecked) {
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//显示
                } else {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//影藏
                }
            }
        });
    }


    /**
     * 正则匹配是否是手机号
     */
    public static Boolean regexPhoneNumber(String s) {
        Pattern p = Pattern.compile(regexPhoneNumber);//获取正则表达式中的分组，每一组小括号为一组
        Matcher m = p.matcher(s);//进行匹配
        return m.find();
    }
}
